package com.wzy.userserviceconsul.controller;

import com.wzy.orderapiconsul.model.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Value("${provider.application.name}")
    private String providerName;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("userGetOrderById")
    public OrderDto userGetOrderById(@RequestParam("orderId") String orderId){
        ResponseEntity<OrderDto> forEntity = restTemplate.getForEntity("http://" + providerName + "/getOrderById?orderId=" + orderId, OrderDto.class);
        return forEntity.getBody();
    }

    @PostMapping("userSaveOrder")
    public OrderDto userSaveOrder(@RequestBody OrderDto orderDto){
        OrderDto forObject = restTemplate.postForObject("http://" + providerName + "/saveOrder", orderDto, OrderDto.class);
        return forObject;
    }
}
